<template>
  <div class="login">
    <div class="login-wrap">
      <ul class="menu-tab">
        <li :class="{'current' : item.current}" v-for="item in menuTab" :key="item.id" @click="menuClick(item)">{{ item.text }}</li>
      </ul>

      <!-- 表单 -->
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForms" size="medium" class="demo-ruleForm">
        <el-form-item prop="userName" class="item-form">
          <label>邮箱</label>
          <el-input type="text" v-model="ruleForm.userName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item prop="password" class="item-form">
          <label>密码</label>
          <el-input type="text" v-model="ruleForm.password" autocomplete="off" minlength="6" maxlength="20"></el-input>
        </el-form-item>

        <el-form-item prop="passwords" class="item-form" v-if="model === 'register'">
          <label>确认密码</label>
          <el-input type="text" v-model="ruleForm.passwords" autocomplete="off" minlength="6" maxlength="20"></el-input>
        </el-form-item>

        <el-form-item prop="code" class="item-form">
          <label>验证码</label>
          <el-row :gutter="10">
            <el-col :span="15">
              <el-input v-model="ruleForm.code" minlength="6" maxlength="6"></el-input>
            </el-col>
            <el-col :span="9">
              <el-button type="success" class="btn" @click="getCode">获取验证码</el-button>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item>
          <el-button type="danger" @click="submitForm('ruleForms')" class="btn login-btn">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { GetCode } from '@/api/login'
import { defineComponent, reactive, ref, isRef, onMounted } from 'vue'
import { stripscript, validateEmail, validatePassword, validateCode } from '@/utils/validate'
export default defineComponent({
  setup(props, context) {
    // 邮箱
    let validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入邮箱'))
      } else if (!validateEmail(value)) {
        callback(new Error('邮箱格式错误'))
      } else {
        callback()
      }
    }
    // 密码
    let validatePass = (rule, value, callback) => {
      // console.log(stripscript(value))
      ruleForm.password = stripscript(value)
      value = ruleForm.password
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (validatePassword(value)) {
        callback(new Error('密码为6至20位数字+字母!'))
      } else {
        callback()
      }
    }
    // 确认密码
    let validatePasss = (rule, value, callback) => {
      // console.log(stripscript(value))
      if (model.value === 'login') { callback() }
      ruleForm.passwords = stripscript(value)
      value = ruleForm.passwords
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value != ruleForm.password) {
        callback(new Error('请保证密码相同'))
      } else {
        callback()
      }
    }
    // 验证码
    let checkCode = (rule, value, callback) => {
      ruleForm.code = stripscript(value)
      value = ruleForm.code
      // let reg = /^[a-z0-9]{6}$/
      if (value === '') {
        return callback(new Error('请输入验证码'))
      } else if (validateCode(value)) {
        callback(new Error('验证码为6位数字+字母!'))
      } else {
        callback()
      }
    }
    // 表单绑定数据
    let ruleForm = reactive({
      userName: '',
      password: '',
      passwords: '',
      code: ''
    })
    // 表单验证
    let rules = reactive({
      userName: [
        { validator: validateName, trigger: 'blur' }
      ],
      password: [
        { validator: validatePass, trigger: 'blur' }
      ],
      passwords: [
        { validator: validatePasss, trigger: 'blur' }
      ],
      code: [
        { validator: checkCode, trigger: 'blur' }
      ]
    })

    // 声明对象用reactive
    const menuTab = reactive([
      { text: '登录', id: 0, current: true, type: 'login' },
      { text: '注册', id: 1, current: false, type: 'register' }
    ])
    // console.log(menuTab)
    // 声明基础类型用 ref
    const model = ref('login')
    const ruleForms = ref(null)
    // console.log(model.value)
    // console.log(isRef(model) ? '基础类型' : '对象类型') // 基础类型

    /**
     * 声明函数
     */
    // 注册登录切换
    const menuClick = (data => {
      menuTab.forEach(ele => {
        ele.current = false
      })
      data.current = true
      model.value = data.type
      console.log(model.value)
    })
    // 获取验证码
    const getCode = (() => {
      let data = {
        username: ruleForm.userName
      }
      GetCode(data)
    })
    // 登录重置
    const submitForm = (() => {
      ruleForms.value.validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    })

    /**
     * 生命周期
     */
    // 挂载完成后
    onMounted(() => {
      // GetCode()
    })

    return {
      menuTab,
      model,
      ruleForm,
      ruleForms,
      rules,
      menuClick,
      getCode,
      submitForm
    }
  }
})
</script>

<style lang="scss" scoped>
  .login {
    height: 100vh;
    background-color: #344a5f;
    color: #fff;
    display: flex;
  }
  .login-wrap {
    width: 330px;
    margin: auto;
    align-self: center;
  }
  .menu-tab {
    text-align: center;
    margin: 0;
    li {
      display: inline-block;
      width: 88px;
      line-height: 36px;
      font-size: 14px;
      color: #fff;
      border-radius: 2px;
      cursor: pointer;
    }
    .current {
      background-color: rgba(0, 0, 0, .1);
    }
    
    .demo-ruleForm {
      display: block;
      font-size: 14px;
      color: #fff;
    }
    .item-form {
      margin-bottom: 13px;
    }
  }
  .btn {
    display: block;
    width: 100%;
  }
  .login-btn {
    margin-top: 19px;
  }
</style>
